
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Java基础 -- 注解 · 寒江蓑笠的博客</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="介绍注解的用法">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="java-basic-threadlocal.html" />
    
    
    <link rel="prev" href="java-basic-joor.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Java
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="java-basic-joor.html">
            
                <a href="java-basic-joor.html">
            
                    
                    Java 基础 -- 使用 JOOR 实现反射
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.1.2" data-path="java-basic-annotation.html">
            
                <a href="java-basic-annotation.html">
            
                    
                    Java基础 -- 注解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="java-basic-threadlocal.html">
            
                <a href="java-basic-threadlocal.html">
            
                    
                    Java 基础 -- ThreadLocal
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="java-development-skills-get-stacktrace.html">
            
                <a href="java-development-skills-get-stacktrace.html">
            
                    
                    Java 开发技巧 -- 获取 Java 的调用栈
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.5" data-path="java-basic-concurrent-map-collection.html">
            
                <a href="java-basic-concurrent-map-collection.html">
            
                    
                    Java 基础 -- 并发集合的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.6" data-path="java-dynamic-proxy-invocationhandler-demo.html">
            
                <a href="java-dynamic-proxy-invocationhandler-demo.html">
            
                    
                    Java InvocationHandler 实现动态代理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.7" data-path="java-basic-try-catch-finally.html">
            
                <a href="java-basic-try-catch-finally.html">
            
                    
                    Java 基础 -- 深入理解 try catch finally
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8" data-path="java-basic-reflect.html">
            
                <a href="java-basic-reflect.html">
            
                    
                    Java 基础 -- 反射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.9" data-path="java-basic-io-stream.html">
            
                <a href="java-basic-io-stream.html">
            
                    
                    Java IO -- IO流的概念和分类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.10" data-path="java-basic-final.html">
            
                <a href="java-basic-final.html">
            
                    
                    Java 基础 -- 深入理解 final 修饰符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.11" data-path="java-basic-memory-manage-constructor.html">
            
                <a href="java-basic-memory-manage-constructor.html">
            
                    
                    Java 内存管理 -- 父子实例的内存控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.12" data-path="java-basic-memory-manage-inherit-object.html">
            
                <a href="java-basic-memory-manage-inherit-object.html">
            
                    
                    Java 内存管理 -- 构造方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.13" data-path="java-basic-memory-manage-memory-area.html">
            
                <a href="java-basic-memory-manage-memory-area.html">
            
                    
                    Java 内存管理 -- 内存区域的划分
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.14" data-path="java-basic-memory-manage-variable.html">
            
                <a href="java-basic-memory-manage-variable.html">
            
                    
                    Java 内存管理 -- 变量的内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.15" data-path="java-basic-knowledge-generic.html">
            
                <a href="java-basic-knowledge-generic.html">
            
                    
                    Java 基础 -- 范型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.16" data-path="java-basic-knowledge-auto-boxing.html">
            
                <a href="java-basic-knowledge-auto-boxing.html">
            
                    
                    Java 基础 -- 关于自动装箱和拆箱机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.17" data-path="java-basic-knowledge-parameter-passing.html">
            
                <a href="java-basic-knowledge-parameter-passing.html">
            
                    
                    Java 基础 -- 值传递、引用传递和参数传递
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.18" data-path="java-basic-stringbuilder-stringbuffer.html">
            
                <a href="java-basic-stringbuilder-stringbuffer.html">
            
                    
                    Java 基础 -- StringBuilder 和 StringBuffer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.19" data-path="java-basic-knowledge-about-string.html">
            
                <a href="java-basic-knowledge-about-string.html">
            
                    
                    Java 基础 -- 深入理解 String
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.20" data-path="java-basic-linkedhashmap.html">
            
                <a href="java-basic-linkedhashmap.html">
            
                    
                    Java 基础 -- LinkedHashMap 的使用和原理
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Java基础 -- 注解</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x6982;&#x8FF0;">&#x6982;&#x8FF0;</h2>
<p>Java &#x4ECE;1.5 &#x7248;&#x672C;&#x540E;&#x5F00;&#x59CB;&#x5F15;&#x5165;&#x4E86;&#x6CE8;&#x89E3;&#xFF08;Annotation&#xFF09;&#xFF0C;&#x5176;&#x76EE;&#x7684;&#x662F;&#x4E0D;&#x5F71;&#x54CD;&#x4EE3;&#x7801;&#x8BED;&#x4E49;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x589E;&#x5F3A;&#x4EE3;&#x7801;&#x7684;&#x53EF;&#x8BFB;&#x6027;&#xFF0C;&#x5E76;&#x4E14;&#x4E0D;&#x6539;&#x53D8;&#x4EE3;&#x7801;&#x7684;&#x6267;&#x884C;&#x903B;&#x8F91;&#x3002;
&#x6CE8;&#x89E3;&#x76F8;&#x5F53;&#x4E8E;&#x4E00;&#x79CD;&#x6807;&#x8BB0;&#xFF0C;&#x5728;&#x7A0B;&#x5E8F;&#x4E2D;&#x52A0;&#x4E0A;&#x4E86;&#x6CE8;&#x89E3;&#x5C31;&#x7B49;&#x4E8E;&#x4E3A;&#x7A0B;&#x5E8F;&#x52A0;&#x4E0A;&#x4E86;&#x67D0;&#x79CD;&#x6807;&#x8BB0;&#xFF0C;&#x4EE5;&#x540E;&#xFF0C;JAVAC&#x7F16;&#x8BD1;&#x5668;&#xFF0C;&#x5F00;&#x53D1;&#x5DE5;&#x5177;&#x548C;&#x5176;&#x4ED6;&#x7A0B;&#x5E8F;&#x53EF;&#x4EE5;&#x7528;&#x53CD;&#x5C04;&#x6765;&#x4E86;&#x89E3;&#x4F60;&#x7684;&#x7C7B;&#x4EE5;&#x53CA;&#x5404;&#x79CD;&#x5143;&#x7D20;&#x4E0A;&#x6709;&#x65E0;&#x4EFB;&#x4F55;&#x6807;&#x8BB0;&#xFF0C;&#x770B;&#x4F60;&#x6709;&#x4EC0;&#x4E48;&#x6807;&#x8BB0;&#xFF0C;&#x5C31;&#x53BB;&#x5E72;&#x76F8;&#x5E94;&#x7684;&#x4E8B;&#x3002;
&#x6CE8;&#x89E3;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x6CE8;&#x89E3;&#x7C7B;&#x3001;&#x65B9;&#x6CD5;&#x3001;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x53C2;&#x6570;&#x7B49;&#x3002;</p>
<p>&#x5176;&#x5B9E;&#x5173;&#x4E8E;&#x6CE8;&#x89E3;&#x7684;&#x4F7F;&#x7528;&#x6709;&#x4E24;&#x6D3E;&#x7684;&#x4E89;&#x8BBA;&#xFF0C;&#x6B63;&#x65B9;&#x8BA4;&#x4E3A;&#x6CE8;&#x89E3;&#x6709;&#x52A9;&#x4E8E;&#x6570;&#x636E;&#x548C;&#x4EE3;&#x7801;&#x7684;&#x8026;&#x5408;&#xFF0C;&#x5728;&#x6709;&#x4EE3;&#x7801;&#x7684;&#x5468;&#x8FB9;&#x96C6;&#x5408;&#x6570;&#x636E;&#xFF0C;&#x53CD;&#x65B9;&#x8BA4;&#x4E3A;&#x6CE8;&#x89E3;&#x628A;&#x4EE3;&#x7801;&#x548C;&#x6570;&#x636E;&#x6DF7;&#x6DC6;&#x5728;&#x4E00;&#x8D77;&#xFF0C;&#x589E;&#x52A0;&#x4E86;&#x4EE3;&#x7801;&#x7684;&#x6613;&#x53D8;&#x6027;&#xFF0C;&#x524A;&#x5F31;&#x4E86;&#x4EE3;&#x7801;&#x7684;&#x5065;&#x58EE;&#x6027;&#x548C;&#x7A33;&#x5B9A;&#x6027;&#x3002;
&#x6211;&#x4EEC;&#x59D1;&#x4E14;&#x4E0D;&#x53BB;&#x770B;&#x8FD9;&#x4E9B;&#x4E89;&#x8BBA;&#xFF0C;&#x5728;&#x5408;&#x9002;&#x7684;&#x5730;&#x65B9;&#x53BB;&#x4F7F;&#x7528;&#x6CE8;&#x89E3;&#xFF0C;&#x786E;&#x5B9E;&#x80FD;&#x7ED9;&#x6211;&#x4EEC;&#x5E26;&#x6765;&#x4E0D;&#x5C11;&#x4FBF;&#x5229;&#x3002;
&#x76EE;&#x524D;&#x770B;&#x5230;&#x7684;&#x5F88;&#x591A;&#x5F00;&#x6E90;&#x6846;&#x67B6;&#x5927;&#x90E8;&#x5206;&#x90FD;&#x4F1A;&#x6709;&#x6CE8;&#x89E3;&#x4F7F;&#x7528;&#x7684;&#x5F71;&#x5B50;&#xFF0C;&#x672C;&#x6587;&#x5C31;&#x7B80;&#x5355;&#x7684;&#x4ECB;&#x7ECD;&#x4E00;&#x4E0B;&#x6CE8;&#x89E3;&#x7684;&#x4F7F;&#x7528;&#x3002;</p>
<h2 id="&#x5E38;&#x7528;&#x6CE8;&#x89E3;">&#x5E38;&#x7528;&#x6CE8;&#x89E3;</h2>
<h3 id="java-&#x6CE8;&#x89E3;">Java &#x6CE8;&#x89E3;</h3>
<ul>
<li>@Override</li>
<li>@JavascriptInterface</li>
<li>@Deprecated</li>
<li>@SuppressWarnings</li>
<li>@Nullable</li>
<li>@NonNull</li>
<li>@StringRes</li>
<li>@DrawableRes</li>
<li>@ColorRes</li>
<li>@InterpolatorRes</li>
<li>@AnyRes</li>
<li>@IntDef</li>
</ul>
<h3 id="android-&#x6CE8;&#x89E3;">Android &#x6CE8;&#x89E3;</h3>
<p>&#x4F7F;&#x7528;&#x65F6;&#x8981;&#x5728;build.gradle&#x4E2D;&#x52A0;&#x4E0A; <code>compile &apos;com.android.support:support-annotations:22.0.0&apos;</code></p>
<ul>
<li>@StringDef</li>
<li>@UiThread</li>
<li>@MainThread</li>
<li>@WorkerThread</li>
<li>@BinderThread</li>
<li>@ColorInt</li>
<li>@Size</li>
<li>@IntRange</li>
<li>@FloatRange</li>
<li>@RequiresPermission</li>
<li>@CallSuper</li>
<li>@CheckResult</li>
<li>@VisibleForTesting</li>
<li>@Keep</li>
<li>@CheckResult</li>
</ul>
<h2 id="&#x5143;&#x6CE8;&#x89E3;">&#x5143;&#x6CE8;&#x89E3;</h2>
<p>&#x5143;&#x6CE8;&#x89E3;&#x662F; java API &#x63D0;&#x4F9B;&#xFF0C;&#x662F;&#x4E13;&#x95E8;&#x7528;&#x6765;&#x5B9A;&#x4E49;&#x6CE8;&#x89E3;&#x7684;&#x6CE8;&#x89E3;&#x3002;
&#x5143;&#x6CE8;&#x89E3;&#x6709;&#x56DB;&#x4E2A;&#xFF1A;</p>
<ul>
<li>@Retention</li>
<li>@Target</li>
<li>@Document</li>
<li>@Inherited</li>
</ul>
<h3 id="retention">Retention</h3>
<pre><code class="lang-java"><span class="hljs-meta">@Documented</span>
<span class="hljs-meta">@Retention</span>(RetentionPolicy.RUNTIME)
<span class="hljs-meta">@Target</span>({ElementType.ANNOTATION_TYPE})
<span class="hljs-keyword">public</span> <span class="hljs-meta">@interface</span> Retention {
    <span class="hljs-function">RetentionPolicy <span class="hljs-title">value</span><span class="hljs-params">()</span></span>;
}
</code></pre>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">enum</span> RetentionPolicy {
    SOURCE,
    CLASS,
    RUNTIME;

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">RetentionPolicy</span><span class="hljs-params">()</span> </span>{
    }
}
</code></pre>
<p><code>Retention</code> &#x8868;&#x793A;&#x8BE5;&#x6CE8;&#x89E3;&#x4FE1;&#x606F;&#x4FDD;&#x5B58;&#x5728;&#x4EC0;&#x4E48;&#x7EA7;&#x522B;&#xFF0C;&#x53EF;&#x9009;&#x7684;&#x53C2;&#x6570;&#x503C;&#x5728;&#x679A;&#x4E3E;&#x7C7B;&#x578B; RetentionPolicy &#x4E2D;&#xFF0C;&#x5305;&#x62EC;&#xFF1A;</p>
<ul>
<li>RetentionPolicy.SOURCE&#xFF1A;&#x5728;&#x7F16;&#x8BD1;&#x9636;&#x6BB5;&#x4E22;&#x5F03;&#x3002;&#x8FD9;&#x4E9B;&#x6CE8;&#x89E3;&#x5728;&#x7F16;&#x8BD1;&#x7ED3;&#x675F;&#x4E4B;&#x540E;&#x5C31;&#x4E0D;&#x518D;&#x6709;&#x4EFB;&#x4F55;&#x610F;&#x4E49;&#xFF0C;&#x6240;&#x4EE5;&#x5B83;&#x4EEC;&#x4E0D;&#x4F1A;&#x5199;&#x5165;&#x5B57;&#x8282;&#x7801;&#x3002;<code>@Override</code>, <code>@SuppressWarnings</code> &#x90FD;&#x5C5E;&#x4E8E;&#x8FD9;&#x7C7B;&#x6CE8;&#x89E3;&#x3002; </li>
<li>RetentionPolicy.CLASS&#xFF1A;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x4EC5;&#x628A;&#x6CE8;&#x89E3;&#x4FDD;&#x5B58;&#x5728; class &#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x5728;&#x8FD0;&#x884C; Java &#x7A0B;&#x5E8F;&#x65F6;&#xFF0C;JVM &#x4E0D;&#x4F1A;&#x4FDD;&#x7559;&#x6CE8;&#x91CA;&#xFF0C;&#x5373;&#x4E0D;&#x80FD;&#x7528;&#x53CD;&#x5C04;&#xFF08;&#x5728;&#x8FD0;&#x884C;&#x671F;&#xFF09;&#x6765;&#x83B7;&#x53D6;&#x6CE8;&#x91CA;&#x3002;<code>@StringRes</code>&#xFF0C;<code>@DrawableRes</code>&#xFF0C;<code>@MainThread</code> &#x90FD;&#x5C5E;&#x4E8E;&#x8FD9;&#x7C7B;&#x6CE8;&#x89E3;&#x3002; </li>
<li>RetentionPolicy.RUNTIME&#xFF1A;&#x7F16;&#x8BD1;&#x5668;&#x4E0D;&#x4EC5;&#x628A;&#x6CE8;&#x89E3;&#x4FDD;&#x5B58;&#x5728; class &#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x540C;&#x65F6;&#x5728;&#x8FD0;&#x884C; Java &#x7A0B;&#x5E8F;&#x65F6;&#xFF0C;JVM &#x4E5F;&#x4F1A;&#x4FDD;&#x7559;&#x6CE8;&#x91CA;&#xFF0C;&#x5373;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x53CD;&#x5C04;&#x6765;&#x83B7;&#x53D6;&#x6CE8;&#x91CA;&#x3002;<code>@Nullable</code>&#xFF0C;<code>@NonNull</code> &#x90FD;&#x5C5E;&#x4E8E;&#x8FD9;&#x7C7B;&#x6CE8;&#x89E3;&#x3002; </li>
</ul>
<h3 id="target">Target</h3>
<pre><code class="lang-java"><span class="hljs-meta">@Documented</span>
<span class="hljs-meta">@Retention</span>(RetentionPolicy.RUNTIME)
<span class="hljs-meta">@Target</span>({ElementType.ANNOTATION_TYPE})
<span class="hljs-keyword">public</span> <span class="hljs-meta">@interface</span> Target {
    ElementType[] value();
}
</code></pre>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">enum</span> ElementType {
    TYPE,
    FIELD,
    METHOD,
    PARAMETER,
    CONSTRUCTOR,
    LOCAL_VARIABLE,
    ANNOTATION_TYPE,
    PACKAGE,
    TYPE_PARAMETER,
    TYPE_USE;

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">ElementType</span><span class="hljs-params">()</span> </span>{
    }
}
</code></pre>
<p><code>Target</code> &#x8868;&#x793A;&#x8BE5;&#x6CE8;&#x89E3;&#x7528;&#x4E8E;&#x4EC0;&#x4E48;&#x5730;&#x65B9;&#xFF0C;&#x53EF;&#x80FD;&#x7684;&#x503C;&#x5728;&#x679A;&#x4E3E;&#x7C7B; ElemenetType &#x4E2D;&#xFF0C;&#x5305;&#x62EC;&#xFF1A;</p>
<ul>
<li>ElemenetType.TYPE&#xFF1A;&#x4FEE;&#x9970;&#x7C7B;&#x3001;&#x63A5;&#x53E3;&#x6216;&#x679A;&#x4E3E;&#xFF08;enum) </li>
<li>ElemenetType.FIELD&#xFF1A;&#x6CE8;&#x89E3;&#x6210;&#x5458;&#x53D8;&#x91CF;</li>
<li>ElemenetType.METHOD&#xFF1A;&#x6CE8;&#x89E3;&#x65B9;&#x6CD5;</li>
<li>ElemenetType.PARAMETER&#xFF1A;&#x6CE8;&#x89E3;&#x65B9;&#x6CD5;&#x53C2;&#x6570; </li>
<li>ElemenetType.CONSTRUCTOR&#xFF1A;&#x6CE8;&#x89E3;&#x6784;&#x9020;&#x51FD;&#x6570;</li>
<li>ElemenetType.LOCAL_VARIABLE&#xFF1A;&#x6CE8;&#x89E3;&#x5C40;&#x90E8;&#x53D8;&#x91CF; </li>
<li>ElemenetType.ANNOTATION_TYPE&#xFF1A;&#x6CE8;&#x89E3;&#x53E6;&#x4E00;&#x4E2A;&#x81EA;&#x5B9A;&#x4E49;&#x6CE8;&#x89E3; </li>
<li>ElemenetType.PACKAGE&#xFF1A;&#x6CE8;&#x89E3;&#x5305;</li>
<li>ElemenetType.TYPE_PARAMETER&#xFF1A;&#x8BE5;&#x6CE8;&#x89E3;&#x80FD;&#x5199;&#x5728;&#x7C7B;&#x578B;&#x53D8;&#x91CF;&#x7684;&#x58F0;&#x660E;&#x8BED;&#x53E5;&#x4E2D;</li>
<li>ElemenetType.TYPE_USE&#xFF1A;&#x8BE5;&#x6CE8;&#x89E3;&#x80FD;&#x5199;&#x5728;&#x4F7F;&#x7528;&#x7C7B;&#x578B;&#x7684;&#x4EFB;&#x4F55;&#x8BED;&#x53E5;&#x4E2D;</li>
</ul>
<h3 id="document">Document</h3>
<p>&#x5C06;&#x6B64;&#x6CE8;&#x89E3;&#x5305;&#x542B;&#x5728; javadoc &#x4E2D; &#xFF0C;&#x5B83;&#x4EE3;&#x8868;&#x7740;&#x6B64;&#x6CE8;&#x89E3;&#x4F1A;&#x88AB;javadoc&#x5DE5;&#x5177;&#x63D0;&#x53D6;&#x6210;&#x6587;&#x6863;&#x3002;&#x5728; doc &#x6587;&#x6863;&#x4E2D;&#x7684;&#x5185;&#x5BB9;&#x4F1A;&#x56E0;&#x4E3A;&#x6B64;&#x6CE8;&#x89E3;&#x7684;&#x4FE1;&#x606F;&#x5185;&#x5BB9;&#x4E0D;&#x540C;&#x800C;&#x4E0D;&#x540C;&#x3002;&#x76F8;&#x5F53;&#x4E0E;@see,@param &#x7B49;&#x3002;</p>
<h3 id="inherited">Inherited</h3>
<p>&#x5141;&#x8BB8;&#x5B50;&#x7C7B;&#x7EE7;&#x627F;&#x7236;&#x7C7B;&#x4E2D;&#x7684;&#x8BE5;&#x6CE8;&#x89E3;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x6807;&#x8BB0;@Inherited&#xFF0C;&#x90A3;&#x4E48;&#x7236;&#x7C7B;&#x7684;&#x8BE5;&#x6CE8;&#x89E3;&#x5B50;&#x7C7B;&#x5C06;&#x65E0;&#x6CD5;&#x7EE7;&#x627F;&#x3002;</p>
<h2 id="&#x81EA;&#x5B9A;&#x4E49;&#x6CE8;&#x89E3;">&#x81EA;&#x5B9A;&#x4E49;&#x6CE8;&#x89E3;</h2>
<p>&#x4F7F;&#x7528;@interface&#x81EA;&#x5B9A;&#x4E49;&#x6CE8;&#x89E3;&#x65F6;&#xFF0C;&#x81EA;&#x52A8;&#x7EE7;&#x627F;&#x4E86; <code>java.lang.annotation.Annotation</code> &#x63A5;&#x53E3;&#xFF0C;&#x7531;&#x7F16;&#x8BD1;&#x7A0B;&#x5E8F;&#x81EA;&#x52A8;&#x5B8C;&#x6210;&#x5176;&#x4ED6;&#x7EC6;&#x8282;&#x3002;&#x5728;&#x5B9A;&#x4E49;&#x6CE8;&#x89E3;&#x65F6;&#xFF0C;&#x4E0D;&#x80FD;&#x7EE7;&#x627F;&#x5176;&#x4ED6;&#x7684;&#x6CE8;&#x89E3;&#x6216;&#x63A5;&#x53E3;&#x3002;<code>@interface</code> &#x7528;&#x6765;&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x6CE8;&#x89E3;&#xFF0C;&#x5176;&#x4E2D;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x58F0;&#x660E;&#x4E86;&#x4E00;&#x4E2A;&#x914D;&#x7F6E;&#x53C2;&#x6570;&#x3002;&#x65B9;&#x6CD5;&#x7684;&#x540D;&#x79F0;&#x5C31;&#x662F;&#x53C2;&#x6570;&#x7684;&#x540D;&#x79F0;&#xFF0C;&#x8FD4;&#x56DE;&#x503C;&#x7C7B;&#x578B;&#x5C31;&#x662F;&#x53C2;&#x6570;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;default&#x6765;&#x58F0;&#x660E;&#x53C2;&#x6570;&#x7684;&#x9ED8;&#x8BA4;&#x503C;&#x3002;
&#x81EA;&#x5B9A;&#x4E49;&#x6CE8;&#x89E3;&#x683C;&#x5F0F;&#xFF1A;
public @interface &#x6CE8;&#x89E3;&#x540D; {&#x5B9A;&#x4E49;&#x4F53;}
&#x6CE8;&#x89E3;&#x53C2;&#x6570;&#x7684;&#x53EF;&#x652F;&#x6301;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF1A;</p>
<ul>
<li>&#x6240;&#x6709;&#x57FA;&#x672C;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF08;int,float,boolean,byte,double,char,long,short)</li>
<li>String&#x7C7B;&#x578B;</li>
<li>Class&#x7C7B;&#x578B;</li>
<li>enum&#x7C7B;&#x578B;</li>
<li>Annotation&#x7C7B;&#x578B;</li>
<li>&#x4EE5;&#x4E0A;&#x6240;&#x6709;&#x7C7B;&#x578B;&#x7684;&#x6570;&#x7EC4;</li>
</ul>
<p>&#x53C2;&#x6570;&#x8BBE;&#x5B9A;&#xFF1A;</p>
<ul>
<li>&#x53EA;&#x80FD;&#x7528;public&#x6216;&#x9ED8;&#x8BA4;(default)&#x8FD9;&#x4E24;&#x4E2A;&#x8BBF;&#x95EE;&#x6743;&#x4FEE;&#x9970;.&#x4F8B;&#x5982;,String value();&#x8FD9;&#x91CC;&#x628A;&#x65B9;&#x6CD5;&#x8BBE;&#x4E3A;defaul&#x9ED8;&#x8BA4;&#x7C7B;&#x578B;&#xFF1B;</li>
<li>&#x53C2;&#x6570;&#x6210;&#x5458;&#x53EA;&#x80FD;&#x7528;&#x4E0A;&#x9762;&#x5217;&#x4E3E;&#x7684;&#x96C6;&#x4E2D;&#x7C7B;&#x578B;</li>
<li>&#x53C2;&#x6570;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x8BBE;&#x7F6E;&#xFF0C;&#x5FC5;&#x987B;&#x5728;&#x6DFB;&#x52A0;&#x6CE8;&#x89E3;&#x65F6;&#x6307;&#x5B9A;&#x3002;</li>
</ul>
<pre><code class="lang-java"><span class="hljs-meta">@Target</span>({ ElementType.TYPE })
<span class="hljs-meta">@Retention</span>(RetentionPolicy.RUNTIME)
<span class="hljs-keyword">public</span> <span class="hljs-meta">@interface</span> Fruit{
    <span class="hljs-function">String <span class="hljs-title">name</span><span class="hljs-params">()</span></span>;
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">grade</span><span class="hljs-params">()</span> <span class="hljs-keyword">default</span> 0</span>;
    <span class="hljs-function">String <span class="hljs-title">country</span><span class="hljs-params">()</span> <span class="hljs-keyword">default</span> &quot;China&quot;</span>;
    String[] saleTo() <span class="hljs-keyword">default</span> {};
}
</code></pre>
<h2 id="&#x6CE8;&#x89E3;&#x89E3;&#x6790;">&#x6CE8;&#x89E3;&#x89E3;&#x6790;</h2>
<p>&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x81EA;&#x5B9A;&#x4E49;&#x6CE8;&#x89E3;&#x7684;&#x76EE;&#x7684;&#x5C31;&#x662F;&#x5BF9;&#x6807;&#x6CE8;&#x7684;&#x7C7B;&#x6216;&#x8005;&#x65B9;&#x6CD5;&#x8FDB;&#x884C;&#x4E00;&#x4E9B;&#x7279;&#x6B8A;&#x5904;&#x7406;&#xFF0C;&#x8FD9;&#x4E2A;&#x5C31;&#x9700;&#x8981;&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x6CE8;&#x89E3;&#x5904;&#x7406;&#x5668;&#x3002;
<code>Class</code> &#x7C7B;&#x63D0;&#x4F9B;&#x4E86;&#x4E0B;&#x9762;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x7C7B;&#x7684;&#x6CE8;&#x89E3;&#x4FE1;&#x606F;&#xFF1A;</p>
<ul>
<li><code>&lt;A extends Annotation&gt; A getAnnotation(Class&lt;A&gt; annotationType)</code>&#xFF1A;&#x83B7;&#x53D6;&#x6307;&#x5B9A;&#x7684;&#x6CE8;&#x89E3;&#xFF0C;&#x8BE5;&#x6CE8;&#x89E3;&#x53EF;&#x4EE5;&#x662F;&#x81EA;&#x5DF1;&#x58F0;&#x660E;&#x7684;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x662F;&#x7EE7;&#x627F;&#x7684;</li>
<li><code>Annotation[] getAnnotations()</code>&#xFF1A;&#x83B7;&#x53D6;&#x6240;&#x6709;&#x7684;&#x6CE8;&#x89E3;&#xFF0C;&#x5305;&#x62EC;&#x81EA;&#x5DF1;&#x58F0;&#x660E;&#x7684;&#x4EE5;&#x53CA;&#x7EE7;&#x627F;&#x7684;</li>
<li><code>Annotation[] getDeclaredAnnotations()</code>&#xFF1A;&#x83B7;&#x53D6;&#x81EA;&#x5DF1;&#x58F0;&#x660E;&#x7684;&#x6CE8;&#x89E3;</li>
<li><code>isAnnotationPresent(Class&lt;? extends Annotation&gt; annotationType)</code>&#xFF1A;&#x68C0;&#x67E5;&#x7C7B;&#x662F;&#x5426;&#x6709;&#x6CE8;&#x89E3;</li>
<li><code>&lt;A extends Annotation&gt; A[] getAnnotationsByType(Class&lt;A&gt; annotationClass)</code></li>
<li><code>&lt;A extends Annotation&gt; A getDeclaredAnnotation(Class&lt;A&gt; var1)</code></li>
</ul>
<p><code>Method</code> &#x7C7B;&#x63D0;&#x4F9B;&#x4E86;&#x4E0B;&#x9762;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x65B9;&#x6CD5;&#x7684;&#x6CE8;&#x89E3;&#x4FE1;&#x606F;&#xFF1A;</p>
<ul>
<li><code>&lt;A extends Annotation&gt; A getAnnotation(Class&lt;A&gt; annotationType)</code></li>
<li><code>Annotation[][] getParameterAnnotations()</code></li>
</ul>
<p><code>Field</code> &#x7C7B;&#x63D0;&#x4F9B;&#x4E86;&#x4E0B;&#x9762;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x5C5E;&#x6027;&#x7684;&#x6CE8;&#x89E3;&#x4FE1;&#x606F;&#xFF1A;</p>
<ul>
<li><code>isAnnotationPresent(Class&lt;? extends Annotation&gt; annotationType)</code></li>
<li><code>&lt;A extends Annotation&gt; A getAnnotation(Class&lt;A&gt; annotationType)</code></li>
<li><code>Annotation[] getDeclaredAnnotations()</code></li>
</ul>
<p>&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code>@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Fruit{
    String name();
    int grade() default 0;
    String country() default &quot;China&quot;;
    String[] saleTo() default {};
}
</code></pre><pre><code>@Fruit(name = &quot;Apple&quot;, saleTo = {&quot;China&quot;,&quot;USA&quot;})
public class TestAnnotation {
    public void commonMethod() {

    }

    @InternalAPI(name = &quot;annotationAPI&quot;, callerList = {&quot;MainActivity&quot;,&quot;Custom&quot;})
    public void annotationAPI(String para) {

    }
}
</code></pre><pre><code>
@Fruit(name = &quot;Apple&quot;, saleTo = {&quot;China&quot;,&quot;USA&quot;})
public class TestAnnotation {
    public void commonMethod() {

    }

    @InternalAPI(name = &quot;annotationAPI&quot;, callerList = {&quot;MainActivity&quot;,&quot;Custom&quot;})
    public void annotationAPI(String para) {

    }
}
</code></pre><h3 id="&#x7C7B;&#x7684;&#x6CE8;&#x89E3;&#x5904;&#x7406;">&#x7C7B;&#x7684;&#x6CE8;&#x89E3;&#x5904;&#x7406;</h3>
<pre><code>    public void classAnnotationProcessor(Object object) {
        Class&lt;?&gt; cls = object.getClass();

        if (cls.isAnnotationPresent(Fruit.class)) {
            Fruit fruit = cls.getAnnotation(Fruit.class);
            if (fruit != null) {
                String name = fruit.name();
                String country = fruit.country();
                int grade = fruit.grade();
                String [] saleTo = fruit.saleTo();

                Log.e(&quot;Test&quot;,&quot;name = &quot;+name+&quot;,country = &quot;+country+&quot;, grade = &quot;+grade+&quot;, saleTo = &quot;+Arrays.toString(saleTo));
            }
        }

    }
</code></pre><h3 id="&#x65B9;&#x6CD5;&#x7684;&#x6CE8;&#x89E3;&#x5904;&#x7406;">&#x65B9;&#x6CD5;&#x7684;&#x6CE8;&#x89E3;&#x5904;&#x7406;</h3>
<pre><code>    public void methodAnnotationProcessor(Object object) {
        Class&lt;?&gt; cls = object.getClass();

        Method[] methods = cls.getDeclaredMethods();

        for (Method method : methods) {
            // &#x83B7;&#x53D6;&#x65B9;&#x6CD5;&#x4FEE;&#x9970;&#x7B26;
            int modifiers = method.getModifiers();
            // &#x83B7;&#x53D6;&#x65B9;&#x6CD5;&#x53C2;&#x6570;&#x5217;&#x8868;
            Class&lt;?&gt;[] parameterTypes = method.getParameterTypes();
            Log.e(&quot;Test&quot;,&quot;modifiers = &quot;+modifiers+&quot;, parameterTypes=&quot;+ Arrays.toString(parameterTypes));
            // &#x83B7;&#x53D6;&#x65B9;&#x6CD5;&#x7684;&#x5236;&#x5B9A;&#x6CE8;&#x89E3;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x88AB;&#x6307;&#x5B9A;&#x6CE8;&#x89E3;&#x4FEE;&#x9970;&#xFF0C;&#x5219;&#x8FD4;&#x56DE; null
            InternalAPI internalAPI = method.getAnnotation(InternalAPI.class);

            if (internalAPI != null) {
                // &#x83B7;&#x53D6;&#x6CE8;&#x89E3;&#x53C2;&#x6570;
                String name = internalAPI.name();
                boolean isHide = internalAPI.isHide();
                String[] list = internalAPI.callerList();
                Log.e(&quot;Test&quot;,&quot;name = &quot;+name+&quot;,isHide = &quot;+isHide+&quot;, caller = &quot;+Arrays.toString(list));
            }
        }
    }
</code></pre><p><strong>&#x6CE8;&#x610F;&#xFF1A;</strong></p>
<ul>
<li>&#x5B50;&#x7C7B;&#x91CD;&#x5199;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x6CE8;&#x89E3;&#x65E0;&#x6CD5;&#x88AB;&#x7EE7;&#x627F;</li>
</ul>
<h2 id="annotationprocessor">annotationProcessor</h2>
<p>annotationProcessor &#x662F;&#x5904;&#x7406;&#x6CE8;&#x89E3;&#x7684;&#x5DE5;&#x5177;&#xFF0C;&#x5B83;&#x7528;&#x6765;&#x5728;&#x7F16;&#x8BD1;&#x65F6;&#x626B;&#x63CF;&#x548C;&#x5904;&#x7406;&#x6CE8;&#x89E3;&#x3002;
&#x5728;&#x7F16;&#x8BD1;&#x7684;&#x65F6;&#x5019;&#x5C31;&#x628A;&#x6CE8;&#x89E3;&#x4FE1;&#x606F;&#x7ED9;&#x4FDD;&#x5B58;&#x4E0B;&#x6765;&#xFF0C;&#x8FD9;&#x5C31;&#x7701;&#x4E86;&#x6211;&#x4EEC;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x5BFB;&#x627E;&#x6CE8;&#x89E3;&#x7684;&#x5F00;&#x9500;&#xFF0C;&#x8FD9;&#x4E5F;&#x662F;&#x6CE8;&#x89E3;&#x7C7B;&#x6846;&#x67B6;&#x975E;&#x5E38;&#x6D41;&#x884C;&#x7684;&#x539F;&#x56E0;&#x3002;
&#x540E;&#x9762;&#x4F1A;&#x6709;&#x4E13;&#x95E8;&#x5199;&#x4E00;&#x7BC7;&#x535A;&#x5BA2;&#x56ED;&#x6765;&#x4ECB;&#x7ECD;&#x3002;</p>
<h2 id="&#x6CE8;&#x89E3;&#x7684;&#x8FD0;&#x7528;">&#x6CE8;&#x89E3;&#x7684;&#x8FD0;&#x7528;</h2>
<h3 id="&#x4F7F;&#x7528;&#x6CE8;&#x89E3;&#x6539;&#x8FDB;&#x4EE3;&#x7801;&#x68C0;&#x67E5;">&#x4F7F;&#x7528;&#x6CE8;&#x89E3;&#x6539;&#x8FDB;&#x4EE3;&#x7801;&#x68C0;&#x67E5;</h3>
<p><a href="https://developer.android.com/studio/write/annotations" target="_blank">Android &#x5B98;&#x65B9;&#x6587;&#x6863;&#xFF1A;&#x4F7F;&#x7528;&#x6CE8;&#x89E3;&#x6539;&#x8FDB;&#x4EE3;&#x7801;&#x68C0;&#x67E5;</a></p>
<h3 id="&#x6CE8;&#x89E3;&#x4EE3;&#x66FF;enum">&#x6CE8;&#x89E3;&#x4EE3;&#x66FF;Enum</h3>
<p><a href="http://www.heqiangfly.com/2013/06/26/java-basic-enum/" target="_blank">&#x6211;&#x7684;&#x535A;&#x5BA2;&#xFF1A;Java &#x57FA;&#x7840; -- enum</a></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="java-basic-joor.html" class="navigation navigation-prev " aria-label="Previous page: Java 基础 -- 使用 JOOR 实现反射">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="java-basic-threadlocal.html" class="navigation navigation-next " aria-label="Next page: Java 基础 -- ThreadLocal">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Java基础 -- 注解","categories":"Java","comments":true,"tags":["Java"],"description":"介绍注解的用法","date":"2016-12-22T10:00:00.000Z","level":"1.1.2","depth":2,"next":{"title":"Java 基础 -- ThreadLocal","level":"1.1.3","depth":2,"path":"source/Java/java-basic-threadlocal.md","ref":"source/Java/java-basic-threadlocal.md","articles":[]},"previous":{"title":"Java 基础 -- 使用 JOOR 实现反射","level":"1.1.1","depth":2,"path":"source/Java/java-basic-joor.md","ref":"source/Java/java-basic-joor.md","articles":[]},"dir":"ltr"},"config":{"plugins":["expandable-chapters","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"expandable-chapters":{},"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"寒江蓑笠的博客","gitbook":"*"},"file":{"path":"source/Java/java-basic-annotation.md","mtime":"2022-05-17T10:49:59.382Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-05-17T10:54:58.084Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

